package es.udc.emarketapp.emarket.model.productFacade;

import javax.sql.DataSource;

import es.udc.emarketapp.emarket.model.product.vo.ProductVO;
import es.udc.emarketapp.emarket.model.productFacade.actions.CreateProductAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.DeleteProductAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.FindByCategoryAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.FindByIdAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.FindByKeywordsAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.FindByKeywordsAndCategoryAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.FindByNameAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.FindByOriginAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.FindBySellerAction;
import es.udc.emarketapp.emarket.model.productFacade.actions.ModifyProductAction;
import es.udc.emarketapp.emarket.model.categoryFacade.vo.CategoryChunkVO;
import es.udc.emarketapp.emarket.model.productFacade.vo.ProductChunkVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.PlainActionProcessor;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;

public class ProductFacade {

    private DataSource dataSource;

    public ProductFacade() throws InternalErrorException {
        dataSource = new SimpleDataSource();
    }

    public ProductVO createProduct(ProductVO productVO)
            throws InternalErrorException {

        try {
            CreateProductAction action = new CreateProductAction(productVO);
            return (ProductVO) PlainActionProcessor.process(
                    dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // createProduct

    public ProductVO modifyProduct(ProductVO productVO)
            throws InternalErrorException {

        try {
            ModifyProductAction action = new ModifyProductAction(productVO);
            return (ProductVO) PlainActionProcessor.process(
                    dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // modifyProduct

    public void deleteProduct(Long idProd)
            throws InstanceNotFoundException, InternalErrorException {

        try {

            DeleteProductAction action = new DeleteProductAction(idProd);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // deleteProduct

    public ProductVO findProductById(Long idProd)
            throws InstanceNotFoundException, InternalErrorException {

        try {
            FindByIdAction action = new FindByIdAction(idProd);
            return (ProductVO) PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // findProductById

    public ProductChunkVO findByCategory(Long idCat,
            int startIndex, int count) throws InternalErrorException {

        try {
            FindByCategoryAction action = new FindByCategoryAction(idCat,
                    startIndex, count);

            return (ProductChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // findByCategory

    public ProductChunkVO findByName(String name,
            int startIndex, int count) throws InternalErrorException {

        try {
            FindByNameAction action = new FindByNameAction(name,
                    startIndex, count);

            return (ProductChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // findByName

    public ProductChunkVO findByKeywords(String keywords,
            int startIndex, int count) throws InternalErrorException {

        try {
            FindByKeywordsAction action = new FindByKeywordsAction(keywords,
                    startIndex, count);

            return (ProductChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // findByKeywords

    public ProductChunkVO findByKeywordsAndCategory(String keywords, Long idCat,
            int startIndex, int count) throws InternalErrorException {

        try {
            FindByKeywordsAndCategoryAction action =
                    new FindByKeywordsAndCategoryAction(keywords, idCat,
                    startIndex, count);

            return (ProductChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // findByKeywordsAndCategory

    public ProductChunkVO findBySeller(String idSeller,
            int startIndex, int count) throws InternalErrorException {

        try {
            FindBySellerAction action = new FindBySellerAction(idSeller,
                    startIndex, count);

            return (ProductChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // findBySeller

    public ProductChunkVO findByOrigin(String paisOrigen,
            int startIndex, int count) throws InternalErrorException {

        try {
            FindByOriginAction action = new FindByOriginAction(paisOrigen,
                    startIndex, count);

            return (ProductChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } //findByOrigin

} // class

